<?php


namespace App\Utility;


use EasySwoole\Component\Singleton;
use EasySwoole\Utility\Hash;

/**
 * 密码的加密和对比（使用php的hash加密）
 * Class Password
 * @package App\Utility
 */
class Password
{
    use Singleton;

    /**
     * 加密
     * @param string $value
     * @param int $cost
     * @param float $timeTarget
     * @return false|string
     */
    public function encryption(string $value, int $cost = 8, float $timeTarget = 0.05)
    {
        do {
            $cost++;
            $start = microtime(true);
            $password = Hash::makePasswordHash($value, $cost);
            $end = microtime(true);
        } while (($end - $start) < $timeTarget);

        return $password;
    }

    /**
     * 验证
     * @param string $value
     * @param string $hashValue
     * @return bool
     */
    public function validate(string $value, string $hashValue): bool
    {
        return Hash::validatePasswordHash($value, $hashValue);
    }

}
